<?php

/**
 * This is the model class for table "{{web_setting}}".
 *
 * The followings are the available columns in table '{{web_setting}}':
 * @property string $id
 * @property string $group_id
 * @property string $key
 * @property string $value
 * @property string $default
 * @property string $type
 * @property string $type_data
 * @property string $description
 */
class WebSetting extends CActiveRecord
{
	const UPLOAD_FOLDER = 'upload/websetting/';
	public static function model($className=__CLASS__) {return parent::model($className);}
	public function tableName() {return '{{web_setting}}';}

	public function rules()
	{
		// Define only rules for those attributes that will receive user inputs.
		return array(
			array('key, value', 'required'),
			array('key','unique'),
			array('group_id', 'numerical', 'integerOnly'=>true),
			array('key, default', 'length', 'max'=>100),
			array('type', 'length', 'max'=>15),
			array('type_data, description', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, key, value, default, type, type_data, group_id, description', 'safe', 'on'=>'search'),
		);
	}

	public function relations()
	{
		return array(
			'group'=>array(self::BELONGS_TO, 'WebSettingGroup', 'group_id'),
		);
	}
	
	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' 		=> Yii::t('websetting', 'ID'),
			'group_id' 	=> Yii::t('websetting', 'Mã nhóm'),
			'group' 	=> Yii::t('websetting', 'Phân nhóm'),
			'key' 		=> Yii::t('websetting', 'Từ khóa'),
			'value' 	=> Yii::t('websetting', 'Giá trị'),
			'default' 	=> Yii::t('websetting', 'Mặc định'),
			'type' 		=> Yii::t('websetting', 'Loại dữ liệu'),
			'type_data' => Yii::t('websetting', 'Mô tả dữ liệu'),
			'description' 	=> Yii::t('websetting', 'Mô tả'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,false);
		$criteria->compare('group_id',$this->group_id,true);
		$criteria->compare('key',$this->key,true);
		$criteria->compare('value',$this->value,true);
		$criteria->compare('default',$this->default,true);
		$criteria->compare('type',$this->type,true);
		$criteria->compare('type_data',$this->type_data,true);
		$criteria->compare('description',$this->description,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
			'sort'=>array('defaultOrder'=>'`key` ASC'),
			'pagination'=>array('pageSize'=>Yii::app()->session->get('adminPageSize',25)),
		));
	}

	public static function TypeList($key=NULL)
	{
		$values = array(
			'textField'=>'textField',
			'fileField'=>'fileField',
			'textArea'=>'textArea',
			'listBox'=>'listBox',
			'checkBox'=>'checkBox',
			'checkBoxList'=>'checkBoxList',
			'dropDownList'=>'dropDownList',
			'radioButton'=>'radioButton',
			'radioButtonList'=>'radioButtonList'
		);

		return $values;
	}

	public function beforeValidate()
	{
		if(parent::beforeValidate())
		{
			if($this->description=="") $this->description = $this->key;
			return true;
		}
		else return false;
	}

	public static function get($key = NULL)
	{
		$criteria = new CDbCriteria;
		$criteria->select = 'value';
		$criteria->condition = '`key`=:key';
		$criteria->params = array(':key'=>$key);
		$model = self::model()->find($criteria);

		if($model===NULL) throw new CHttpException(404, Yii::t('websetting', 'Cài đặt: {key}; Yêu cầu không hợp lệ hoặc thông tin đã thay đổi, vui lòng liên hệ quản trị website để được hướng dẫn.', array('{key}'=>$key)));
		return $model->value;
	}

	public static function set($key, $value=NULL)
	{
		$criteria = new CDbCriteria;
		$criteria->condition = '`key`=:key';
		$criteria->params = array(':key'=>$key);
		$model = self::model()->find($criteria);

		$value===NULL?Yii::t('websetting','Dữ liệu mẫu, hãy dùng chức năng quản trị để cập nhật.'):$value;
		if($model===NULL) 
		{
			$model = new WebSetting;
			$model->key = $key;
			$model->value = $value;
			$model->save();
		}
		else
		{
			$model->value = $value;
			$model->save();
		}
		
		return $model;
	}
	
	public function beforeDelete()
	{
		if(parent::beforeDelete())
		{
			if($this->type=='fileField')
				@unlink($this->value);
			return true;
		} else return false;
	}
}